package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.Service.OrderSettingService;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        //判断集合是否有数据。
        if (list != null && list.size() > 0){
            //有数据则遍历
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行预约设置
                long countByOrderDate=orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate>0){
                    //已经进行更新操作，执行更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //没有进行更新操作，执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    //根据月份查询对应的预约数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用DAO，查询预约设置数据
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        List<Map> re = new ArrayList<>();
        if (list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> o = new HashMap<>();
                o.put("date",orderSetting.getOrderDate().getDate());
                o.put("number",orderSetting.getNumber());
                o.put("reservations",orderSetting.getReservations());
                re.add(o);
            }
        }
        return re;
    }

    //根据日期设置对应数据
    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //查询日期是否已经进行预约设置
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
        if (countByOrderDate>0){
            //已经进行了预约设置，更新预约
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //没有进行预约设置，插入操作
            orderSettingDao.add(orderSetting);
        }
    }
}
